package com.browsesoft;

import java.awt.HeadlessException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * 系统配置窗口
 */
public class SystemConfigFrame extends JFrame implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1204251044141777364L;

	LinkedList configs = new LinkedList();

	Element element;

	static FileService file;

	public SystemConfigFrame(Element e, String path) throws HeadlessException {
		this.element = e;
		this.init();
		JPanel panel = new JPanel();
		this.getContentPane().add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		this.configs = this.getConfig(e, path);
		Iterator iter = configs.iterator();
		while (iter.hasNext()) {
			SystemConfig item = (SystemConfig) iter.next();
			panel.add(item);
		}
		panel = new JPanel();
		JButton confirm = new JButton("确认");
		confirm.addMouseListener(this);
		this.getContentPane().add("South", panel);
		panel.add(confirm);
		// panel.add(cancel);
	}

	private void init() {
		String title = this.get("title");
		this.setTitle(title);
		try {
			int height = Integer.parseInt(get("height"));
			int width = Integer.parseInt(get("width"));
			this.setSize(width, height);
		} catch (NumberFormatException ex) {
			this.setSize(500, 400);
		}
	}

	private String get(String name) {
		return this.element.getAttribute(name);
	}

	public static void main(String[] args) throws Exception {
		String path = args[0];
		file = new FileService(path + "/systemconfig.xml");
		SystemConfigFrame frame = new SystemConfigFrame(file.getDocument()
				.getDocumentElement(), path);
		frame.setVisible(true);
	}

	private LinkedList getConfig(Element e, String path) {
		LinkedList result = new LinkedList();
		NodeList nodeList = e.getElementsByTagName("config");
		for (int i = 0; i < nodeList.getLength(); i++) {
			Element config = (Element) nodeList.item(i);
			result.add(new SystemConfig(config, path));
		}
		return result;
	}

	/**
	 * Invoked when the mouse button has been clicked (pressed and released) on
	 * a component.
	 */
	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			Iterator iter = this.configs.iterator();
			while (iter.hasNext()) {
				SystemConfig item = (SystemConfig) iter.next();
				try {
					item.save();
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(this, "保存文件时发生下面的错误:\n"
							+ ex.getMessage(), "保存文件错误",
							JOptionPane.ERROR_MESSAGE);
				}
			}
			try {
				OutputStream os = new FileOutputStream(file.getFile());
				os.write("<?xml version='1.0' encoding='GB2312'?>".getBytes());
				os.write(file.getDocument().getDocumentElement().toString()
						.getBytes());
				os.flush();
				os.close();
			} catch (IOException ex1) {
				JOptionPane
						.showMessageDialog(this, "保存文件时发生下面的错误:\n"
								+ ex1.getMessage(), "保存文件错误",
								JOptionPane.ERROR_MESSAGE);
			}
			JOptionPane.showMessageDialog(this, "系统配置已经改变,重启服务系统后生效。");
			System.exit(0);
		}
	}

	/**
	 * Invoked when a mouse button has been pressed on a component.
	 */
	public void mousePressed(MouseEvent e) {
	}

	/**
	 * Invoked when a mouse button has been released on a component.
	 */
	public void mouseReleased(MouseEvent e) {
	}

	/**
	 * Invoked when the mouse enters a component.
	 */
	public void mouseEntered(MouseEvent e) {
	}

	/**
	 * Invoked when the mouse exits a component.
	 */
	public void mouseExited(MouseEvent e) {
	}
}